package com.ruoyi.web.controller.company.study;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.company.uploadfile.domain.SysFileInfo;
import com.ruoyi.company.uploadfile.service.ISysFileInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.company.study.domain.ComStudy;
import com.ruoyi.company.study.service.IComStudyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学习培训管理Controller
 * 
 * @author tzn
 * @date 2020-12-21
 */
@Controller
@RequestMapping("/company/study")
public class ComStudyController extends BaseController
{
    private String prefix = "company/study";

    @Autowired
    private IComStudyService comStudyService;
    @Autowired
    private ISysFileInfoService sysFileInfoService;

    @RequiresPermissions("company:study:view")
    @GetMapping()
    public String study()
    {
        return prefix + "/study";
    }

    /**
     * 查询学习培训管理列表
     */
    @RequiresPermissions("company:study:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ComStudy comStudy)
    {
        startPage();
        List<ComStudy> list = comStudyService.selectComStudyList(comStudy);
        return getDataTable(list);
    }

    /**
     * 导出学习培训管理列表
     */
    @RequiresPermissions("company:study:export")
    @Log(title = "学习培训管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ComStudy comStudy)
    {
        List<ComStudy> list = comStudyService.selectComStudyList(comStudy);
        ExcelUtil<ComStudy> util = new ExcelUtil<ComStudy>(ComStudy.class);
        return util.exportExcel(list, "study");
    }

    /**
     * 新增学习培训管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学习培训管理
     */
    @RequiresPermissions("company:study:add")
    @Log(title = "学习培训管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ComStudy comStudy)
    {
        comStudy.setStudyId(IdUtils.simpleUUID());
        comStudy.setCreateUser(ShiroUtils.getLoginName());
        return toAjax(comStudyService.insertComStudy(comStudy));
    }

    /**
     * 上传附件学习培训管理
     */
    @RequiresPermissions("company:study:upload")
    @GetMapping("/upload/{studyId}/{model}")
    public String upload(@PathVariable("studyId") String studyId, @PathVariable("model") String model, ModelMap mmap)
    {
        mmap.put("studyId", studyId);
        mmap.put("model", model);
        return prefix + "/upload";
    }


    /**
     * 上传附件保存学习培训
     */
    @RequiresPermissions("company:study:add")
    @Log(title = "学习培训", businessType = BusinessType.INSERT)
    @PostMapping("/UploadAdd")
    @ResponseBody
    public AjaxResult UploadSave(@RequestParam("file") MultipartFile file, @RequestParam(value="studyId",required=false) String studyId, @RequestParam(value="modelName",required=false) String modelName) throws IOException {
        SysFileInfo sysFileInfo = new SysFileInfo();
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        sysFileInfo.setFileId(IdUtils.simpleUUID());
        sysFileInfo.setFilePath(fileName);
        //sysFileInfo.setCreateUser(ShiroUtils.getLoginName());
        sysFileInfo.setFileName(StringUtils.substringBefore(file.getOriginalFilename(),"."));
        sysFileInfo.setFileType(StringUtils.substringAfter(file.getOriginalFilename(),"."));
        sysFileInfo.setRelationId(studyId);
        sysFileInfo.setModelName(modelName);
        return toAjax(sysFileInfoService.insertSysFileInfo(sysFileInfo));

    }
    /**
     * 修改学习培训管理
     */
    @GetMapping("/edit/{studyId}")
    public String edit(@PathVariable("studyId") String studyId, ModelMap mmap)
    {
        ComStudy comStudy = comStudyService.selectComStudyById(studyId);
        mmap.put("comStudy", comStudy);
        return prefix + "/edit";
    }

    /**
     * 修改保存学习培训管理
     */
    @RequiresPermissions("company:study:edit")
    @Log(title = "学习培训管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ComStudy comStudy)
    {
        return toAjax(comStudyService.updateComStudy(comStudy));
    }

    /**
     * 删除学习培训管理
     */
    @RequiresPermissions("company:study:remove")
    @Log(title = "学习培训管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(comStudyService.deleteComStudyByIds(ids));
    }
}
